package cn.lumora.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lumora.mapper.CharacterBaseInfoMapper;
import cn.lumora.pojo.CharacterBaseInfo;
import cn.lumora.service.CharacterBaseInfoService;
import cn.lumora.service.CharacterInitService;
import cn.lumora.vo.CharacterBaseInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CharacterBaseInfoServiceImpl extends ServiceImpl<CharacterBaseInfoMapper, CharacterBaseInfo> implements CharacterBaseInfoService {

    private final CharacterBaseInfoMapper characterBaseInfoMapper;

    private final CharacterInitService  characterInitService;

    public CharacterBaseInfoServiceImpl(CharacterBaseInfoMapper characterBaseInfoMapper, CharacterInitService characterInitService) {
        this.characterBaseInfoMapper = characterBaseInfoMapper;
        this.characterInitService = characterInitService;
    }

    /**
     * 获取当前用户的角色列表
     */
    @Override
    public List<CharacterBaseInfo> getPlayerRoleList() {
        try {
            log.info("当前请求中的 Token：{}", StpUtil.getTokenValue());
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            // 获取登录账号
            String loginId = tokenInfo.getLoginId().toString();
            LambdaQueryWrapper<CharacterBaseInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CharacterBaseInfo::getPlayerId, loginId);
            return list(queryWrapper);
        } catch (Exception e) {
            log.error("获取当前用户的角色列表失败", e);
            return List.of();
        }
    }

    /**
     * 获取角色数据
     *
     * @param characterId 角色id
     * @return 角色数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CharacterBaseInfoVo getCharacterBaseInfo(String characterId) {
        return characterBaseInfoMapper.getCharacterBaseInfo(characterId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String login(String characterId) {
        try {
            // 根据characterId查询角色数据
            CharacterBaseInfo characterBaseInfo = getById(characterId);
            if (ObjectUtil.isEmpty(characterBaseInfo)) {
                throw new RuntimeException("角色不存在");
            }
            characterBaseInfo.setLastLoginTime(LocalDateTime.now());
            characterBaseInfo.setUpdateTime(LocalDateTime.now());
            updateById(characterBaseInfo);
            return characterId;
        } catch (Exception e) {
            log.error("角色登录失败", e);
            return null;
        }
    }

    /**
     * 创建角色
     *
     * @return 角色id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCharacter() {
        try {
            // 获取当前登录账号
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            String loginId = tokenInfo.getLoginId().toString();
            // 创建角色
            CharacterBaseInfo characterBaseInfo = CharacterBaseInfo.builder()
                    .playerId(loginId)
                    .name("角色" + System.currentTimeMillis())
                    .level(1) // 初始等级为1级
                    .exp(0L) // 初始经验为0
                    .maxExp(100L) // 初始最大经验为100
                    .healthPoints(100) // 初始血量为100
                    .attackPower(10) // 初始攻击力为10
                    .defensePower(10) // 初始防御力为10
                    .gold(100) // 初始金币为100
                    .cultivationRate(0) // 初始培养度为0
                    .build();
            this.save(characterBaseInfo);
            // 初始化角色属性
            initCharacterInfo(characterBaseInfo.getId());


        } catch (Exception e) {
            log.error("创建角色失败", e);
            throw new RuntimeException("创建角色失败");
        }
        return "";
    }

    /**
     * 初始化角色属性
     */
    private void initCharacterInfo(String characterId) {
        // 初始化天赋
        characterInitService.initCharacterTalent(characterId);
        // 初始化角色背包
        characterInitService.initCharacterInventory(characterId);
        // 初始化境界
        characterInitService.initCultivationStage(characterId);
    }
}
